package ml.uuku.oschub.ui;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import ml.uuku.oschub.AppManager;
import ml.uuku.oschub.R;
import ml.uuku.oschub.interf.BaseViewInterface;
import ml.uuku.oschub.widget.MyFragmentTabHost;

public class MainActivity extends ActionBarActivity implements
        NavigationDrawableFragment.NavigationDrawerCallback, BaseViewInterface, View.OnClickListener,
        TabHost.OnTabChangeListener {

    private NavigationDrawableFragment mNavigationDrawableFragment;

    @Bind(android.R.id.tabhost)
    MyFragmentTabHost mTabHost;

    private CharSequence mTitle;

    @Bind(R.id.quick_option_iv)
    View mQuickOption;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
        AppManager.getAppManager().addActivity(this);
    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {

    }

    @Override
    public void initView() {

        mNavigationDrawableFragment = (NavigationDrawableFragment) getSupportFragmentManager().findFragmentById(
                R.id.navigation_drawer);
        mNavigationDrawableFragment.setup(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
        mTitle = getTitle();

        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);

        initTabs();

        mQuickOption.setOnClickListener(this);

        mTabHost.setCurrentTab(0);
        mTabHost.setOnTabChangedListener(this);

    }

    private void initTabs() {
        MainTab[] tabs = MainTab.values();
        int size = tabs.length;
        for (int i = 0; i < size; i++) {
            MainTab mainTab = tabs[i];
            TabHost.TabSpec tab = mTabHost.newTabSpec(getString(mainTab.getResName()));
            View indicator = View.inflate(this, R.layout.tab_indicator, null);
            TextView title = (TextView) indicator.findViewById(R.id.tab_title);
            Drawable icon = getResources().getDrawable(mainTab.getResIcon());
            title.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null);

            if (i == 2) {
                // 快速操作按钮没有Tab内容
                indicator.setVisibility(View.INVISIBLE);
                mTabHost.setNoTabChangedTag(getString(mainTab.getResName()));
            }
            title.setText(getString(mainTab.getResName()));
            tab.setIndicator(indicator);
            tab.setContent(new TabHost.TabContentFactory() {
                @Override
                public View createTabContent(String tag) {
                    return new View(MainActivity.this);
                }
            });
            mTabHost.addTab(tab, mainTab.getClz(), null);
        }
    }

    @Override
    public void initData() {
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onTabChanged(String tabId) {

        int size = mTabHost.getTabWidget().getTabCount();
        for (int i = 0; i < size; i++) {
            View v = mTabHost.getTabWidget().getChildAt(i);
            if (i == mTabHost.getCurrentTab()) {
                v.setSelected(true);
            } else {
                v.setSelected(false);
            }
        }

    }
}
